package cn.iocoder.an.module.business.controller.admin.auditplanarea;

import com.fhs.core.trans.anno.TransMethodResult;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;

import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;

import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.business.controller.admin.auditplanarea.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.auditplanarea.AuditPlanAreaDO;
import cn.iocoder.an.module.business.service.auditplanarea.AuditPlanAreaService;

@Tag(name = "管理后台 - 审核计划区域")
@RestController
@RequestMapping("/business/audit-plan-area")
@Validated
public class AuditPlanAreaController {

    @Resource
    private AuditPlanAreaService auditPlanAreaService;

    @PostMapping("/create")
    @Operation(summary = "创建审核计划区域")
    @PreAuthorize("@ss.hasPermission('business:audit-plan-area:create')")
    public CommonResult<Long> createAuditPlanArea(@Valid @RequestBody AuditPlanAreaSaveReqVO createReqVO) {
        return success(auditPlanAreaService.createAuditPlanArea(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新审核计划区域")
    @PreAuthorize("@ss.hasPermission('business:audit-plan-area:update')")
    public CommonResult<Boolean> updateAuditPlanArea(@Valid @RequestBody AuditPlanAreaSaveReqVO updateReqVO) {
        auditPlanAreaService.updateAuditPlanArea(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除审核计划区域")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:audit-plan-area:delete')")
    public CommonResult<Boolean> deleteAuditPlanArea(@RequestParam("id") Long id) {
        auditPlanAreaService.deleteAuditPlanArea(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除审核计划区域")
    @PreAuthorize("@ss.hasPermission('business:audit-plan-area:delete')")
    public CommonResult<Boolean> deleteAuditPlanAreaList(@RequestParam("ids") List<Long> ids) {
        auditPlanAreaService.deleteAuditPlanAreaListByIds(ids);
        return success(true);
    }


    @TransMethodResult
    @GetMapping("/get")
    @Operation(summary = "获得审核计划区域")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:audit-plan-area:query')")
    public CommonResult<AuditPlanAreaRespVO> getAuditPlanArea(@RequestParam("id") Long id) {
        return success(auditPlanAreaService.getAuditPlanArea(id));
    }

    @TransMethodResult
    @GetMapping("/page")
    @Operation(summary = "获得审核计划区域分页")
    @PreAuthorize("@ss.hasPermission('business:audit-plan-area:query')")
    public CommonResult<PageResult<AuditPlanAreaRespVO>> getAuditPlanAreaPage(@Valid AuditPlanAreaPageReqVO pageReqVO) {
        PageResult<AuditPlanAreaDO> pageResult = auditPlanAreaService.getAuditPlanAreaPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AuditPlanAreaRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出审核计划区域 Excel")
    @PreAuthorize("@ss.hasPermission('business:audit-plan-area:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAuditPlanAreaExcel(@Valid AuditPlanAreaPageReqVO pageReqVO,
                                         HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AuditPlanAreaDO> list = auditPlanAreaService.getAuditPlanAreaPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "审核计划区域.xls", "数据", AuditPlanAreaRespVO.class,
                BeanUtils.toBean(list, AuditPlanAreaRespVO.class));
    }

}